package leecode

import (
	"container/list"
	"math"
)

func largestValues(root *TreeNode) (result []int) {
	if root == nil {
		return
	}
	queue := list.New()
	queue.PushBack(root)
	for queue.Len() != 0 {
		size := queue.Len()
		temp := math.MinInt
		for i := 0; i < size; i++ {
			//将队列头移除队列
			node := queue.Remove(queue.Front()).(*TreeNode) //Remove的返回值是移除的节点
			if node.Val > temp {
				temp = node.Val
			}
			if node.Left != nil {
				queue.PushBack(node.Left)
			}
			if node.Right != nil {
				queue.PushBack(node.Right)
			}
		}
		result = append(result, temp)
	}
	return
}
